package edu.umn.cs5115.scheduler;

/*
 * Create a Tree with columns
 * 
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 * 
 * @since 3.1
 */

import edu.umn.cs5115.scheduler.entities.Weekday;
import edu.umn.cs5115.scheduler.search.SearchResults;
import edu.umn.cs5115.scheduler.util.WidgetUtil;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Set;
import java.util.Vector;

import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.events.TreeEvent;
import org.eclipse.swt.events.TreeListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.*;

import edu.umn.cs5115.scheduler.*;
import edu.umn.cs5115.scheduler.entities.*;
import edu.umn.cs5115.scheduler.framework.*;
import edu.umn.cs5115.scheduler.framework.gui.SelectionController;

public class LeftPanel extends Composite {
	
	private GridLayout mainLayout;
	
	private ConsideringList consideringList;
	private SchedulerDocument document;
	
	private String [] DeptsShort; 
	
	public LeftPanel(Composite parent, SchedulerDocument document) {
		super(parent, SWT.NONE);
		this.document = document;
		
		WebInterface webInterface = WebInterface.getWebInterface();
    	ArrayList<String> deptList = webInterface.getDepartments();
	    DeptsShort = new String [deptList.size()];
	    for (int i = 0; i < DeptsShort.length; ++i)
	    {
	    	int index1 = deptList.get(i).indexOf(" - ");
	    	int index2 = index1 + 3;
	    	DeptsShort[i] = deptList.get(i).substring(index2, deptList.get(i).length());
	    }
		
		initializeWidgets();
	}
	
	public void initializeWidgets() {
		this.mainLayout = new GridLayout();
		this.mainLayout.numColumns = 1;
		this.setLayout(this.mainLayout);
		
		// create the quick course add group
		Group quickAdd = new Group(this, SWT.BORDER);
		quickAdd.setLayout(new GridLayout(1, false));
		quickAdd.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
		
		// create the quick course widgets
		
		// top and bottom rows
		Composite topRow = new Composite(quickAdd, SWT.NONE);
		Composite bottomRow = new Composite(quickAdd, SWT.NONE);
		topRow.setLayout(new GridLayout(3, true));
		bottomRow.setLayout(new GridLayout(2, true));
		topRow.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
		bottomRow.setLayoutData(new GridData(SWT.FILL, SWT.BOTTOM, true, false));
		
		// top row widgets ("add course", dept, coursenum)
		Label addCourseLbl      = new Label(topRow, SWT.NONE);
		final Text deptTxt      = new Text (topRow, SWT.BORDER);
		final Text courseNumTxt = new Text (topRow, SWT.BORDER);
		addCourseLbl.setText      ("Add Course");
		addCourseLbl.setLayoutData( new GridData(SWT.CENTER, SWT.CENTER, false, false));
		deptTxt.setLayoutData     ( new GridData(SWT.FILL, SWT.TOP, true, false));
		courseNumTxt.setLayoutData( new GridData(SWT.FILL, SWT.TOP, false, false));
		
		// caption labels
		Label emptyLabel       = new Label(topRow, SWT.NONE);
		Label deptCaption      = new Label(topRow, SWT.NONE);
		Label courseNumCaption = new Label(topRow, SWT.NONE);
		emptyLabel.setText      ("");
		deptCaption.setText     ("Dept.");
		courseNumCaption.setText("Course #");
		
		// "More Options" and "Search" buttons
		Button advSearchBtn = new Button(bottomRow, SWT.PUSH);
		final Button addBtn       = new Button(bottomRow, SWT.PUSH);
		advSearchBtn.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, true, false));
		addBtn.setLayoutData(new GridData(SWT.END, SWT.TOP, true, false));
		advSearchBtn.setText("More Options");
		addBtn.setText      ("Search");
		
		getShell().setDefaultButton(addBtn);
                
		advSearchBtn.addListener(SWT.Selection, new Listener () {
			public void handleEvent (Event event) {
				document.getSearchWindowController().showWindow();
			}
		});
		
		addBtn.addListener(SWT.Selection, new Listener()
		{
			public void handleEvent(Event event)
			{
				// dept must be in uppercase or no results will be returned
				deptTxt.setText(deptTxt.getText().toUpperCase());
				
				WebInterface newpage = WebInterface.getWebInterface();
				SearchQuery query = new SearchQuery();
				
				query.addSubject(deptTxt.getText());
                                // add a * to the end of the course number search
                                // query so that if they type "3081" they will
                                // see "3081W"
                                if (courseNumTxt.getText().indexOf("*") == -1)
                                    query.searchCourseNumber(courseNumTxt.getText() + "*");
				SearchResults results = new SearchResults(query, document);
				document.getSearchWindowController().showSearchResults(results, false);
				document.getSearchWindowController().showWindow();
			}
		});
		
		// handling blank searchs and auto-completion
		
		addBtn.setEnabled(false);
		courseNumTxt.addListener(SWT.KeyUp, new Listener () {
			public void handleEvent (Event event) {
				if (deptTxt.getText().length() == 0 && courseNumTxt.getText().length() == 0)
				{
					addBtn.setEnabled(false);
				}
				else
				{
					addBtn.setEnabled(true);
				}
			}
		});
		    	
		deptTxt.addListener(SWT.KeyUp, new Listener () {
			public void handleEvent (Event event) {
				if (deptTxt.getText().length() == 0 && courseNumTxt.getText().length() == 0)
				{
					addBtn.setEnabled(false);
				}
				else
				{
					addBtn.setEnabled(true);
				}
				if ((event.keyCode == 8) || (event.keyCode == 127))
					return;
				int length = deptTxt.getText().length();
				if (deptTxt.getSelection().equals(new Point(length, length)))
				{
					String current = deptTxt.getText();
					for (int i = 0; i < DeptsShort.length; ++i)
					{
						if (length >= DeptsShort[i].length())
							continue;
						if (current.equalsIgnoreCase(DeptsShort[i].substring(0,length)))
						{
							deptTxt.setText(DeptsShort[i]);
							deptTxt.setSelection(new Point(addBtn.getText().length(), length));
							addBtn.setEnabled(true);
							return;
						}
					}
				}
			}
		});
		
		// init course list
		//ConsideringList list = new ConsideringList(this, SWT.NONE, document);
		edu.umn.cs5115.scheduler.courselist.CoursesList list = new edu.umn.cs5115.scheduler.courselist.CoursesList(this, document);
		GridData listData = new GridData(SWT.FILL, SWT.FILL, true, true);
		list.setLayoutData(listData);
	}

}